<template>
  <div class="app-container">
    <!-- 查询项目信息列表 -->
    <el-form
      :model="queryParams"
      ref="queryForm"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="项目名称" prop="proName">
        <el-input
          v-model="queryParams.proName"
          placeholder="请输入项目名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <!-- 操作按钮 -->
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['dmsIms:info:add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['dmsIms:info:edit']"
          >修改</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['dmsIms:info:remove']"
          >删除</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['dmsIms:info:export']"
          >导出</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <!-- 数据表格展示 -->
    <el-table
      v-loading="loading"
      :data="infoList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="项目id" align="center" prop="id" width="60" />
      <el-table-column label="项目名称" align="center" prop="proName" />
      <el-table-column label="项目状态" align="center" prop="proStatus">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.proStatus === '1'" type="info">未开始</el-tag>
          <el-tag v-else-if="scope.row.proStatus === '2'">进行中</el-tag>
          <el-tag v-else-if="scope.row.proStatus === '3'" type="success"
            >已完成</el-tag
          >
          <el-tag v-else type="danger">其它</el-tag>
        </template>
      </el-table-column>
      <el-table-column
        label="项目详情"
        align="center"
        prop="proDetail"
        show-overflow-tooltip
      />
      <el-table-column
        label="项目备注"
        align="center"
        prop="proRemarks"
        show-overflow-tooltip
      />
      <!-- <el-table-column
        label="Request Header"
        align="center"
        prop="proHeader"
        show-overflow-tooltip
      />
      <el-table-column
        label="项目地址"
        align="center"
        prop="proBaseurl"
        show-overflow-tooltip
      /> -->
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-search"
            @click="handleDetail(scope.row)"
            >查看</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['dmsIms:info:edit']"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['dmsIms:info:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 查询项目信息对话框 -->
    <el-dialog
      :title="title"
      :visible.sync="searchopen"
      width="680px"
      append-to-body
    >
      <el-descriptions
        class="margin-top"
        :title="form.proName"
        :column="1"
        size="medium"
        :border="true"
        :labelStyle="{ width: '80px' }"
      >
        <el-descriptions-item label="项目名称">
          {{ form.proName }}
        </el-descriptions-item>
        <el-descriptions-item label="项目详情">
          <el-input
            v-model="form.proDetail"
            type="textarea"
            :autosize="{ minRows: 4, maxRows: 6 }"
            disabled
          />
        </el-descriptions-item>
        <el-descriptions-item label="项目状态">
          <template>
            <el-tag v-if="form.proStatus === '1'" type="info">未开始</el-tag>
            <el-tag v-else-if="form.proStatus === '2'">进行中</el-tag>
            <el-tag v-else-if="form.proStatus === '3'" type="success">
              已完成
            </el-tag>
            <el-tag v-else type="danger">其它</el-tag>
          </template>
        </el-descriptions-item>
        <el-descriptions-item label="项目备注">
          <el-input
            v-model="form.proRemarks"
            type="textarea"
            :autosize="{ minRows: 4, maxRows: 6 }"
            disabled
          />
        </el-descriptions-item>
        <el-descriptions-item label="Request Header">
          <el-input
            v-model="form.proHeader"
            type="textarea"
            :autosize="{ minRows: 4, maxRows: 6 }"
            disabled
          />
        </el-descriptions-item>
        <!-- <el-descriptions-item label="项目地址">
          {{ form.proBaseurl }}
        </el-descriptions-item> -->
      </el-descriptions>
    </el-dialog>

    <!-- 新增或修改项目信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="680px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="项目名称" prop="proName">
          <el-input
            v-model="form.proName"
            placeholder="请输入项目名称"
            maxlength="30"
            show-word-limit
          />
        </el-form-item>
        <el-form-item label="项目详情" prop="proDetail">
          <el-input
            v-model="form.proDetail"
            type="textarea"
            placeholder="请输入内容"
            maxlength="200"
            show-word-limit
            :autosize="{ minRows: 3, maxRows: 5 }"
          />
        </el-form-item>
        <el-form-item label="项目状态" prop="proStatus">
          <el-radio-group v-model="form.proStatus">
            <el-radio
              v-for="dict in dict.type.pro_status"
              :key="dict.value"
              :label="dict.value"
            >
              {{ dict.label }}
            </el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="项目备注" prop="proRemarks">
          <el-input
            v-model="form.proRemarks"
            type="textarea"
            placeholder="请输入项目备注"
            maxlength="200"
            show-word-limit
            :autosize="{ minRows: 3, maxRows: 5 }"
          />
        </el-form-item>
        <!-- <el-form-item label="Request Header" prop="proHeader">
          <el-input
            v-model="form.proHeader"
            type="textarea"
            :autosize="{ minRows: 4, maxRows: 6 }"
            placeholder="请输入内容"
          />
        </el-form-item>
        <el-form-item label="项目地址" prop="proBaseurl">
          <el-input v-model="form.proBaseurl" placeholder="请输入项目地址" />
        </el-form-item> -->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm" v-preventReClick
          >确 定</el-button
        >
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listInfo,
  getInfo,
  delInfo,
  addInfo,
  updateInfo,
  canDel,
} from "@/api/dmsIms/info";

export default {
  name: "Info",
  dicts: ["pro_status"],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 项目信息表格数据
      infoList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      searchopen: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        proName: null,
      },
      // 表单参数
      form: {
        id: null,
        proName: null,
        proStatus: "1",
        proDetail: null,
        proRemarks: null,
        proHeader: `accept: */*
connection: Keep-Alive
user-agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)`,
        proBaseurl: null,
        status: 0,
        createTime: null,
        updateTime: null,
      },
      // 表单校验
      rules: {
        proName: [
          { required: true, message: "项目名称不能为空", trigger: "blur" },
        ],
        proDetail: [
          { required: true, message: "项目详情不能为空", trigger: "blur" },
        ],
        proBaseurl: [
          { required: true, message: "项目地址不能为空", trigger: "blur" },
        ],
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询项目信息列表 */
    getList() {
      this.loading = true;
      listInfo(this.queryParams).then((response) => {
        this.infoList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        proName: null,
        proStatus: "1",
        proDetail: null,
        proRemarks: null,
        proHeader: `accept: */*
connection: Keep-Alive
user-agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)`,
        proBaseurl: null,
        status: 0,
        createTime: null,
        updateTime: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 查询按钮操作 */
    handleDetail(row) {
      this.reset();
      const id = row.id || this.ids;
      getInfo(id).then((response) => {
        this.form = response.data;
        this.searchopen = true;
        this.title = "项目信息";
      });
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加项目信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getInfo(id).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改项目信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            updateInfo(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addInfo(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      // 查询是否有计划和接口，先要删除计划和接口
      const ids = row.id || this.ids;
      canDel(ids).then((response) => {
        let { planCount, interCount } = response.data;
        if (planCount === 0 && interCount === 0) {
          this.$modal
            .confirm('是否确认删除项目信息编号为"' + ids + '"的数据项？')
            .then(function () {
              return delInfo(ids);
            })
            .then(() => {
              this.getList();
              this.$modal.msgSuccess("删除成功");
            })
            .catch(() => {});
        } else {
          this.$message({
            message: `项目中还存在${
              planCount === 0 ? "" : planCount + "个关联的测试计划和"
            }
            ${
              interCount === 0 ? "" : interCount + "个关联的测试接口"
            }，不能删除！`,
            type: "error",
          });
        }
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "dmsIms/info/export",
        {
          ...this.queryParams,
        },
        `info_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
